package io.renren.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.admin.dao.CourseDao;
import io.renren.modules.admin.entity.CourseEntity;
import io.renren.modules.admin.service.CourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {

    @Autowired
    private CourseDao courseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String type=(String) params.get("type");
        String fId=(String) params.get("fId");
        String isLogin=(String) params.get("isLogin");
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();
        if ("1".equals(isLogin)){
            wrapper.select("course_imgs");
        }
        wrapper.eq(StringUtils.isNotBlank(type),"type",type);
        wrapper.eq(StringUtils.isNotBlank(fId),"f_id",fId);
        wrapper.orderByDesc("order_num");

        IPage<CourseEntity> page = this.page(new Query<CourseEntity>().getPage(params),wrapper);

        return new PageUtils(page);
    }



    @Override
    public List<CourseEntity> selectByWrapper(Map<String, Object> params) {
        String type=(String) params.get("type");
        String fId=(String) params.get("fId");
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();

        wrapper.eq(StringUtils.isNotBlank(type),"type",type);
        wrapper.eq(StringUtils.isNotBlank(fId),"f_id",fId);
        wrapper.eq("side","0");
        List<CourseEntity> courseEntities = courseDao.selectList(wrapper);

        return courseEntities;
    }

    @Override
    public void createCourse(CourseEntity entity) {
        this.save(entity); //主键返回
        if(entity.getType()==0){
            //创建上册
            CourseEntity courseUp = new CourseEntity();
            courseUp.setFId(entity.getCourseId());
            courseUp.setSide(entity.getSide());
            courseUp.setType(1);
            courseUp.setCourseTitle("上册");
            this.save(courseUp);

            //创建下册
            CourseEntity courseDown = new CourseEntity();
            courseDown.setType(1);
            courseDown.setCourseTitle("下册");
            courseDown.setFId(entity.getCourseId());
            courseDown.setSide(entity.getSide());
            this.save(courseDown);
        }
    }

    @Override
    public PageUtils getCourseList(Map<String, Object> params) {
        IPage<CourseEntity> page = new Query<CourseEntity>().getPage(params);

        Long userId = Long.valueOf(params.get("userId").toString());
        List<CourseEntity> userCourses = courseDao.getUserCourse(page, userId);

        page.setRecords(userCourses);
        return new PageUtils(page);
    }

    @Override
    public List<CourseEntity> teacherCourseProgram(Long courseId) {

        //CourseEntity courseEntity = courseDao.selectById(courseId);
        QueryWrapper<CourseEntity> courseEntityQueryWrapper = new QueryWrapper<>();
        courseEntityQueryWrapper.eq("f_id",courseId);
        List<CourseEntity> courseEntities = courseDao.selectList(courseEntityQueryWrapper);
        //courseEntity.setChildren(courseEntities);

        return courseEntities;
    }


}